package pl.edu.pjwstk.kor.sbql4j.project2;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project2Query_SbqlQuery5Db4o0 implements Db4oSBQLQuery {
    public Project2Query_SbqlQuery5Db4o0() {
    }

    /**
     * query='db.(Customer as c where c.getRanks().getGroupName() != "Partner biznesowy" and  count(c.getDeals()) >= 5)'
    '
     **/
    public java.util.Collection<pl.edu.pjwstk.kor.model.Customer> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Customer> _ident_Customer =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Customer>();
        ClassMetadata _classMeta5 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Customer");
        long[] _ids5 = _classMeta5.getIDs(transLocal);

        for (long _id5 : _ids5) {
            LazyObjectReference _ref5 = transLocal.lazyReferenceFor((int) _id5);
            _ident_Customer.add((pl.edu.pjwstk.kor.model.Customer) _ref5.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Customer> _asResult_c = _ident_Customer;
        java.util.Collection<pl.edu.pjwstk.kor.model.Customer> _whereResult = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Customer>();
        int _whereLoopIndex = 0;

        for (pl.edu.pjwstk.kor.model.Customer _whereEl : _asResult_c) {
            if (_whereEl == null) {
                continue;
            }

            if (_whereEl != null) {
                ocb.activate(_whereEl, 1);
            }

            pl.edu.pjwstk.kor.model.Customer _ident_c = _whereEl;

            if (_ident_c != null) {
                ocb.activate(_ident_c, 1);
            }

            pl.edu.pjwstk.kor.model.Customer _dotEl = _ident_c;

            if (_ident_c != null) {
                ocb.activate(_ident_c, 2);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Rank> _mth_getRanksResult = _dotEl.getRanks();

            if (_mth_getRanksResult != null) {
                ocb.activate(_mth_getRanksResult, 2);
            }

            java.util.List<java.lang.String> _dotResult1 = new java.util.ArrayList<java.lang.String>();
            int _dotIndex1 = 0;

            for (pl.edu.pjwstk.kor.model.Rank _dotEl1 : _mth_getRanksResult) {
                if (_dotEl1 == null) {
                    continue;
                }

                if (_dotEl1 != null) {
                    ocb.activate(_dotEl1, 1);
                }

                java.lang.String _mth_getGroupNameResult = _dotEl1.getGroupName();

                if (_mth_getGroupNameResult != null) {
                    ocb.activate(_mth_getGroupNameResult, 1);
                }

                if (_mth_getGroupNameResult != null) {
                    ocb.activate(_mth_getGroupNameResult, 1);
                }

                _dotResult1.add(_mth_getGroupNameResult);
                _dotIndex1++;
            }

            java.lang.Boolean _not_equalsResult = !OperatorUtils.equalsSafe(_dotResult1,
                    "Partner biznesowy");
            java.lang.Boolean _andResult;

            if (!_not_equalsResult) {
                _andResult = false;
            } else {
                pl.edu.pjwstk.kor.model.Customer _ident_c1 = _whereEl;

                if (_ident_c1 != null) {
                    ocb.activate(_ident_c1, 1);
                }

                pl.edu.pjwstk.kor.model.Customer _dotEl2 = _ident_c1;

                if (_ident_c1 != null) {
                    ocb.activate(_ident_c1, 2);
                }

                java.util.List<pl.edu.pjwstk.kor.model.Deal> _mth_getDealsResult =
                    _dotEl2.getDeals();

                if (_mth_getDealsResult != null) {
                    ocb.activate(_mth_getDealsResult, 2);
                }

                java.lang.Integer _countResult = _mth_getDealsResult.size();

                Boolean _more_or_equalResult = _countResult >= 5;
                _andResult = _more_or_equalResult;
            }

            if (_andResult) {
                _whereResult.add(_whereEl);
            }

            _whereLoopIndex++;
        }

        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_whereResult, ocb);

        return _whereResult;
    }
}
